package cn.edu.glut.user.repository;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import cn.edu.glut.entity.Student;
import cn.edu.glut.repository.BatchRepository;
import cn.edu.glut.vo.StudentToAdminVO;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月18日下午4:05:26
 * @email Caizhfy@163.com
 * @description
 *
 */
@Repository
public interface StudentRepository
		extends JpaRepository<Student, String>,/* JpaSpecificationExecutor<Student>,*/ BatchRepository<Student> {
	Student findByUsername(String username);
	
	Page<StudentToAdminVO> findAllStudentToAdminVOPage(Pageable pageable, String majorId, Boolean isSelect, Boolean isSelectSuccess);

	@Query("select new cn.edu.glut.vo.StudentToAdminVO(s.id, s.username, s.nickname, s.sclass, m.name as majorName, cast(count(se.id) as int) as choose, cast(max(se.isSelection) as int) as selectSuccess) from Student as s "
			+ "left join s.selections as se "
			+ "left join s.major as m "
			+ "where s.username=:username group by s.id ")
	StudentToAdminVO findStudentToAdminVOByUsername(@Param("username")String username);

	//部分字段映射为全部集合
	/*@Query("select s.id, s.username, s.nickname, m.name, s.sclass from Student s "
			+ " inner join s.selections se "
			+ " left join s.major m where se.subject.id=:subjectId")*/
	@Query("select new cn.edu.glut.vo.StudentToAdminVO(s.id, s.username, s.nickname, s.sclass, m.name as majorName, cast(max(se.isSelection) as int) as selectSuccess) from Student as s "
		  + " inner join s.selections se "
		  + " left join s.major m where se.subject.id=:subjectId")
	List<StudentToAdminVO> findAllBySubjectId(@Param("subjectId")String subjectId);
}
